﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Lucene.Linq.Search;

namespace Lucene.Linq.Demo2
{
    internal class Program
    {
        private static IIndex<Country> _countryIndex;
        private static List<Country> _countryList;

        private static void Main(string[] args)
        {
            try
            {
                /*
                 * 
                 * index objects with attributes
                 * 
                 * Still need a small adapter for Linq to XML though :<
                 * 
                 */
                _countryList = new List<Country>();
                _countryIndex = new Index<Country>();

                _countryList.AddRange(new CountryXmlReader().Execute());
                _countryIndex.Add(_countryList);

                Console.WriteLine("Added {0}", _countryIndex.Count);
                Console.WriteLine("======================================");

                SkipDemo();
                PagingDemo();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.WriteLine("[enter] to exit");
                Console.ReadLine();
            }
        }


        private static void PagingDemo()
        {
            Console.WriteLine("=========================================");
            Console.WriteLine("===========PAGING DEMO ==================");

            IQueryable<Country> query = (from c in _countryIndex
                                         where c.TwoLetterCode.StartsWith("A")
                                         select c);


            // To Paged List demos count, skip and take
            PagedList<Country> results = query.ToPagedList(1, 5);


            Console.WriteLine("Page {0}/{1}, Page Size : {2}",
                              results.PageNumber,
                              results.PageCount,
                              results.PageSize);
            Console.WriteLine("Count: {0}, Total Count : {1}", results.Count, results.TotalItemCount);
            Console.WriteLine("Page Count : {0}", results.PageCount);

            foreach (Country a in results)
                Console.WriteLine(a.Name);
        }


        private static void SkipDemo()
        {
            Console.WriteLine("=========================================");
            Console.WriteLine("===========SKIP DEMO ==================");

            IQueryable<Country> query = (from c in _countryIndex
                                         where c.TwoLetterCode.StartsWith("B")
                                         select c);

            var queryList = (from c in _countryList
                             where c.TwoLetterCode.StartsWith("B")
                             select c);

            Console.WriteLine("Total list count :{0}", queryList.Count());
            Console.WriteLine("Total query count :{0}", query.Count());

            IEnumerable<Country> results = query.Skip(3).Skip(3).Skip(3).Take(5);

            IEnumerable<Country> resultsList = queryList.Skip(3).Skip(3).Skip(3).Take(5);

            Console.WriteLine("Query count after skipping 9 and taking 5 :{0}", results.Count());
            Console.WriteLine("List count after skipping 9 and taking 5 :{0}", resultsList.Count());

            // check each index result is in the list result
            foreach (var c in results)
            {
                Debug.Assert(resultsList.Contains(c, new CountryComparer()), "!!!ERROR, results don't match");
            }

            Console.WriteLine("======== INDEX ");
            foreach (var a in results)
                Console.WriteLine(a.Name);

            Console.WriteLine("======== LIST ");
            foreach (var a in resultsList)
                Console.WriteLine(a.Name);
        }
    }
}